import http from "../utils/http";
import { reactive } from "vue";

const cart = reactive({
  course_list: [], // 购物车列表
  total_price: 0, // 总价
  selected_course_total: 0, // 选中的课程总数
  checked: false, // 是否全选
  select_course_list: [], // 选中的课程列表
  // 添加商品到购物车
  add_course_to_cart(course_id, token) {
    return http.post(
      "/cart/",
      {
        course_id,
      },
      {
        headers: {
          Authorization: "jwt " + token,
        },
      }
    );
  },
  // 获取购物车列表
  get_course_from_cart(token) {
    return http.get("/cart/", {
      headers: {
        Authorization: "jwt " + token,
      },
    });
  },

  select_course(course_id, selected, token) {
    // 切换指定商品课程的勾选状态
    return http.patch(
      "/cart/",
      {
        course_id,
        selected,
      },
      {
        headers: {
          Authorization: "jwt " + token,
        },
      }
    );
  },
  select_all_course(selected, token) {
    // 切换购物车对应商品课程的全选状态
    return http.put(
      "/cart/",
      {
        selected,
      },
      {
        headers: {
          Authorization: "jwt " + token,
        },
      }
    );
  },
  delete_course(course_id, token) {
    // 从购物车中删除商品课程
    return http.delete("/cart/", {
      params: {
        course_id,
      },
      headers: {
        Authorization: "jwt " + token,
      },
    });
  },
  get_selected(token) {
    // 获取购物车中已选择商品课程
    return http.get("/cart/select/", {
      headers: {
        Authorization: "jwt " + token,
      },
    });
  },
});

export default cart;
